package com.jhx.common.widget;

import android.content.Context;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import com.jhx.common.R;
import com.jhx.common.utils.DisplayUtil;

/**
 * 公共toolbar
 *
 * @author jack 2017-05-17 11:06
 */
public class CommonToolbar extends Toolbar {
    private TextView mTitleView;
    private Context  mContext;

    public CommonToolbar(Context context) {
        this(context, null);
    }

    public CommonToolbar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CommonToolbar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setContentInsetsAbsolute(0, 0);
        setPadding(DisplayUtil.dip2px(context, 10),
                   DisplayUtil.dip2px(context, 10),
                   DisplayUtil.dip2px(context, 10),
                   DisplayUtil.dip2px(context, 10));
        setNavigationIcon(R.mipmap.icon_back_white);
        mContext = context;
    }

    @Deprecated
    @Override
    public void setTitle(CharSequence title) {
        super.setTitle(title);
    }

    public void setTitle(String title) {
        if(TextUtils.isEmpty(title)) {
            return;
        }
        mTitleView = new TextView(mContext);
        LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        layoutParams.gravity = Gravity.CENTER;
        mTitleView.setLayoutParams(layoutParams);
        mTitleView.setText(title);
        addView(mTitleView);
    }

    public void setMenuView(View view) {
        if(view == null) {
            return;
        }
        LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        layoutParams.gravity = Gravity.CENTER_VERTICAL | Gravity.RIGHT;
        view.setLayoutParams(layoutParams);
        addView(view);
    }

    public TextView getTitleView() {
        return mTitleView;
    }

    public void setTitleView(TextView titleView) {
        mTitleView = titleView;
    }
}
